package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Organization;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.response.YearAndQuarter;
import com.xmy.cultivate.respons.ResSchool;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 组织机构 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-07-26
 */
public interface OrganizationMapper extends BaseMapper<Organization> {

    @Select("")
    public abstract List<Organization> getOrganiztionForApoint();

    @Select("select id from organization ${ew.customSqlSegment}")
    public abstract List<Long> getOrganiztionId(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select name from organization where id = #{school_id}")
    public abstract String getSchoolName(@Param("school_id") Long schoolId);

    @Select("select level from organization where id = #{school_id}")
    public abstract Integer getSchoolLevel(@Param("school_id") String schoolId);

    @Select("SELECT id,name,level FROM organization WHERE type = 1 AND status = 1 AND deleted = 0")
    public abstract List<Organization> getIdAndName();

    @Select({"<script>",
            "SELECT",
            "school_id as id,",
            "getSchoolName ( school_id ) AS name ",
            "FROM",
            "grade ",
            "WHERE",
            "deleted = 0 ",
            "<when test='yearAndQuarterList != null and yearAndQuarterList.size() > 0'> ",
            "AND (",
            "<foreach collection='yearAndQuarterList' item='item' open='(' separator='or' close=')'>",
            "`year_part` = ${item.yearPart}",
            "and quarter_num =${item.quarterNum}",
            "</foreach>",
            ")",
            "</when> ",
            "GROUP BY",
            "school_id",
            "</script>"})
    public List<ResSchool> getSchoolForYearAndNum(@Param("yearAndQuarterList") List<YearAndQuarter> yearAndQuarterList);

    @Select("SELECT t1.schoolmaster_id FROM `organization` AS t1 INNER JOIN school_quarter AS t2 ON t1.id = t2.school_id WHERE t2.year_part = ${yearPart} AND t2.quarter_num = ${quarterNum} AND t2.deleted = 0 AND t1.schoolmaster_id IS NOT NULL")
    public abstract List<Long> getSchoolmasterIdList(@Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum);

    @Select("SELECT t1.schoolmaster_id FROM `organization` AS t1 INNER JOIN school_quarter AS t2 ON t1.id = t2.school_id WHERE t2.year_part = ${yearPart} AND t2.quarter_num = ${quarterNum} AND t2.deleted = 0 AND t1.schoolmaster_id IS NOT NULL AND t1.schoolmaster_id = ${adminId} GROUP BY t1.schoolmaster_id ")
    public abstract String schoolmasterIdByAdminId(@Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum, @Param("adminId") Long adminId);
    @Select("select count(t1.id) as count from organization as t1 INNER JOIN school_quarter as t2 on t1.id = t2.school_id where t2.year_part = ${yearPart} and t2.quarter_num = ${quarterNum} and t1.schoolmaster_id = ${teacherId}")
    public Integer getIsManager(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,@Param("teacherId") Long teacherId);


}
